package com.ruoyi.web.controller.modules;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.incomeexpense.IncomeExpense;
import com.ruoyi.web.pojo.dto.incomeexpense.IncomeExpensePage;
import com.ruoyi.web.pojo.dto.incomeexpense.ProportionListVo;
import com.ruoyi.web.pojo.request.incomeexpense.IEDetailsReq;
import com.ruoyi.web.pojo.request.incomeexpense.IEPageReq;
import com.ruoyi.web.pojo.request.incomeexpense.IncomeExpenseAdd;
import com.ruoyi.web.service.incomeexpense.IIncomeExpenseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * 支入支出Controller
 * 
 * @author zq
 * &#064;date  2024-01-09
 */
@Api(tags = "支入支出")
@RestController
@RequestMapping("/income/expense")
public class IncomeExpenseController extends BaseController
{
    private final IIncomeExpenseService incomeExpenseService;
    public IncomeExpenseController(IIncomeExpenseService incomeExpenseService) {
        this.incomeExpenseService = incomeExpenseService;
    }

    /**
     * 查询支入支出列表
     */
    @ApiOperation("PC查询支入支出分页")
    @PostMapping("/list")
    public TableDataInfo<IncomeExpensePage> list(@RequestBody IEPageReq req)
    {
        startPage();
        List<IncomeExpensePage> list = incomeExpenseService.selectIncomeExpenseList(req);
        return getDataTable(list);
    }

    @ApiOperation("小程序查询列表")
    @PostMapping("/wx/detailsList")
    public AjaxResult wxList(@RequestBody IEDetailsReq req){
        ProportionListVo list = incomeExpenseService.selectDetailsListVo(req);
        return AjaxResult.success(list);
    }

    @ApiOperation("小程序首页（总利润）")
    @GetMapping("/wx/homeprofit")
    public AjaxResult homeProfit(){
        Map<String, Object> map = incomeExpenseService.countIncomeExpenseMoney();
        return success(map);
    }


    @ApiOperation("小程序：总利润查询")
    @PostMapping("/wx/profit")
    public AjaxResult profit(@RequestBody IEDetailsReq req) {
        Map<String, Object> profit = incomeExpenseService.profit(req);
        return AjaxResult.success(profit);
    }

    /**zq
     * 导出支入支出列表
     */
    @ApiOperation("小程序：导出")
    @PostMapping("/wx/export")
    public AjaxResult export(@RequestBody IEDetailsReq req, HttpServletResponse response) {
        return success(incomeExpenseService.export(req));
    }

    /**
     * 导出zq支入支出列表
     */
    @ApiOperation("导出支入支出列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody IEPageReq incomeExpense) throws Exception {
        List<IncomeExpensePage> list = incomeExpenseService.selectIncomeExpenseList(incomeExpense);
        ExcelUtil<IncomeExpensePage> util = new ExcelUtil<>(IncomeExpensePage.class);
        util.exportExcel(response, list, "支入支出数据");
    }

    /**
     * 获取支入支出详细信息zq
     */
    @ApiOperation("获取支入支出详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(incomeExpenseService.selectIncomeExpenseById(id));
    }

    /**zq
     * 新增支入支出zq
     */
    @ApiOperation("新增支入支出")
    @PostMapping("insert")
    public AjaxResult add(@RequestBody @Valid IncomeExpenseAdd req)
    {
        return toAjax(incomeExpenseService.insertIncomeExpense(IncomeExpense.toIncomeExpense(req)));
    }

    /**zq
     * 修改支入支出
     */
    @ApiOperation("修改支入支出")
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable Long id,@RequestBody IncomeExpenseAdd req)
    {
        IncomeExpense in = IncomeExpense.toIncomeExpense(req);
        in.setId(id);
        return toAjax(incomeExpenseService.updateIncomeExpense(in));
    }

    /**zq
     * 删除支入支出
     */
    @ApiOperation("删除支入支出")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(incomeExpenseService.deleteIncomeExpenseById(id));
    }
}
